﻿/* **************************************************************************
 *
 * Copyright 2007-2010 Jeff Hardy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * *************************************************************************/

using Microsoft.Web.Administration;

namespace NWSGI.Management
{
    public enum FrameOptions
    {
        Off, On, Full
    }

    public enum CompilationOptions
    {
        Adaptive, Compiled
    }

    class WsgiSection : ConfigurationSection
    {
        public static readonly string SectionName = "wsgi";

        public bool EnableExtensions
        {
            get { return base["enableExtensions"] as bool? ?? false; }
            set { base["enableExtensions"] = value; }
        }

        public CompilationOptions Compilation
        {
            get { return base["compilation"] as CompilationOptions? ?? (int)CompilationOptions.Adaptive; }
            set { base["compilation"] = value; }
        }

        public FrameOptions Frames
        {
            get { return (FrameOptions)(base["frames"] as int? ?? (int)FrameOptions.Off); }
            set { base["frames"] = (int)value; }
        }

        public bool Tracing
        {
            get { return base["tracing"] as bool? ?? false; }
            set { base["tracing"] = value; }
        }

        public bool LightweightScopes
        {
            get { return base["lightweightScopes"] as bool? ?? false; }
            set { base["lightweightScopes"] = value; }
        }

        public bool EnableProfiler
        {
            get { return base["enableProfiler"] as bool? ?? false; }
            set { base["enableProfiler"] = value; }
        }

        public WildcardElement Wildcard
        {
            get{ return (WildcardElement)base.GetChildElement("wildcard", typeof(WildcardElement)); }
        }

        private PythonPathCollection pythonPaths;
        public PythonPathCollection PythonPaths
        {
            get
            {
                if (this.pythonPaths == null)
                {
                    this.pythonPaths = (PythonPathCollection)base.GetCollection("pythonPaths", typeof(PythonPathCollection));
                }

                return this.pythonPaths;
            }
        }

        private WsgiEnvironCollection wsgiEnviron;
        public WsgiEnvironCollection WsgiEnviron
        {
            get
            {
                if(this.wsgiEnviron == null)
                {
                    this.wsgiEnviron = (WsgiEnvironCollection)base.GetCollection("wsgiEnviron", typeof(WsgiEnvironCollection));
                }

                return this.wsgiEnviron;
            }
        }

        private ScriptMappingCollection scriptMappings;
        public ScriptMappingCollection ScriptMappings
        {
            get
            {
                if(this.scriptMappings == null)
                {
                    this.scriptMappings = (ScriptMappingCollection)base.GetCollection("scriptMappings", typeof(ScriptMappingCollection));
                }

                return this.scriptMappings;
            }
        }
    }

    #region Wildcard Settings

    class WildcardElement : ConfigurationElement
    {
        public static readonly string PhysicalPathAttribute = "physicalPath";
        public static readonly string CallableAttribute = "callable";

        public string PhysicalPath
        {
            get { return (string)base[PhysicalPathAttribute]; }
            set { base[PhysicalPathAttribute] = value; }
        }

        public string Callable
        {
            get { return (string)base[CallableAttribute]; }
            set { base[CallableAttribute] = value; }
        }
    }

    #endregion

    #region Python Paths

    class PythonPathInfo : ConfigurationElement
    {
        public static readonly string PathAttribute = "path";

        public string Path
        {
            get { return (string)base[PathAttribute]; }
            set { base[PathAttribute] = value; }
        }
    }

    class PythonPathCollection : ConfigurationElementCollectionBase<PythonPathInfo>
    {
        protected override PythonPathInfo CreateNewElement(string elementTagName)
        {
            return new PythonPathInfo();
        }
    }

    #endregion

    #region WSGI Environ

    class WsgiEnvironElement : ConfigurationElement
    {
        public static readonly string NameAttribute = "name";
        public static readonly string ValueAttribute = "value";

        public string Name
        {
            get { return (string)base[NameAttribute]; }
            set { base[NameAttribute] = value; }
        }

        public string Value
        {
            get { return (string)base[ValueAttribute]; }
            set { base[ValueAttribute] = value; }
        }
    }

    class WsgiEnvironCollection : ConfigurationElementCollectionBase<WsgiEnvironElement>
    {
        protected override WsgiEnvironElement CreateNewElement(string elementTagName)
        {
            return new WsgiEnvironElement();
        }
    }

    #endregion

    #region Script Mapping

    class ScriptMappingElement : ConfigurationElement
    {
        public static readonly string ScriptNameAttribute = "scriptName";
        public static readonly string PhysicalPathAttribute = "physicalPath";
        public static readonly string CallableAttribute = "callable";

        public string ScriptName
        {
            get { return (string)base[ScriptNameAttribute]; }
            set { base[ScriptNameAttribute] = value; }
        }

        public string PhysicalPath
        {
            get { return (string)base[PhysicalPathAttribute]; }
            set { base[PhysicalPathAttribute] = value; }
        }

        public string Callable
        {
            get { return (string)base[CallableAttribute]; }
            set { base[CallableAttribute] = value; }
        }
    }

    class ScriptMappingCollection : ConfigurationElementCollectionBase<ScriptMappingElement>
    {
        protected override ScriptMappingElement CreateNewElement(string elementTagName)
        {
            return new ScriptMappingElement();
        }
    }

    #endregion
}
